import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.*;
import java.util.Date;

public class SimpleLogsManageFrame extends JFrame {
    private JTable logsTable;
    private DefaultTableModel tableModel;
    private JComboBox<String> tableFilter;
    private JComboBox<String> typeFilter;
    private JButton refreshButton;

    public SimpleLogsManageFrame() {
        setTitle("操作日志管理");
        setSize(800, 400);
        setLocationRelativeTo(null);

        // 初始化简化表格模型
        tableModel = new DefaultTableModel();
        tableModel.addColumn("操作表");
        tableModel.addColumn("操作类型");
        tableModel.addColumn("操作时间");

        logsTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(logsTable);

        // 初始化简化筛选面板
        JPanel filterPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));

        filterPanel.add(new JLabel("操作表:"));
        tableFilter = new JComboBox<>(new String[]{"全部", "goods", "buy", "offer", "sale", "customer", "warehouse"});
        filterPanel.add(tableFilter);

        filterPanel.add(new JLabel("操作类型:"));
        typeFilter = new JComboBox<>(new String[]{"全部", "INSERT", "UPDATE", "DELETE"});
        filterPanel.add(typeFilter);

        refreshButton = new JButton("查询");
        refreshButton.addActionListener(this::loadLogsData);
        filterPanel.add(refreshButton);

        // 设置布局
        setLayout(new BorderLayout());
        add(filterPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);

        // 加载初始数据
        loadLogsData();
    }

    private void loadLogsData(ActionEvent e) {
        loadLogsData();
    }

    private void loadLogsData() {
        tableModel.setRowCount(0); // 清空表格

        String selectedTable = (String) tableFilter.getSelectedItem();
        String selectedType = (String) typeFilter.getSelectedItem();

        StringBuilder sql = new StringBuilder(
                "SELECT table_name, operation_type, operation_time " +
                        "FROM operation_logs " +
                        "WHERE table_name IN ('goods', 'buy', 'offer', 'sale', 'customer', 'warehouse')"
        );

        if (!"全部".equals(selectedTable)) {
            sql.append(" AND table_name = '").append(selectedTable).append("'");
        }

        if (!"全部".equals(selectedType)) {
            sql.append(" AND operation_type = '").append(selectedType).append("'");
        }

        sql.append(" ORDER BY operation_time DESC");

        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql.toString())) {

            while (rs.next()) {
                tableModel.addRow(new Object[]{
                        rs.getString("table_name"),
                        rs.getString("operation_type"),
                        rs.getTimestamp("operation_time")
                });
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this,
                    "加载日志失败: " + e.getMessage(),
                    "错误",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}